import validation from '@/utils/libs/validation/validation.js'
import Utils from '@/utils'
export default class WxPay {
  constructor() {}

  requestPayment(res) {
    return new Promise((resolve, reject) => {
      const param = {
        provider: 'wxpay',
        signType: res.signType,
        timeStamp: res.timeStamp,
        nonceStr: res.nonceStr,
        package: res.package,
        paySign: res.paySign
      }

      const rules = [
        { name: 'provider', rule: ['required'], msg: ['provider 为空'] },
        { name: 'timeStamp', rule: ['required'], msg: ['timeStamp 为空'] },
        { name: 'nonceStr', rule: ['required'], msg: ['nonceStr 为空'] },
        { name: 'package', rule: ['required'], msg: ['package 为空'] },
        { name: 'signType', rule: ['required'], msg: ['signType 为空'] },
        { name: 'paySign', rule: ['required'], msg: ['paySign 为空'] }
      ]

      const checkRes = validation.validate(param, rules)

      if (checkRes) {
        Utils.msg('text', checkRes)
        return reject(checkRes)
      }
      console.param('requestPayment', param)
      uni.requestPayment({
        ...param,
        success: res => {
          console.res('requestPayment', res)
          resolve(res)
        },
        fail: err => {
          console.err('requestPayment', res)
          reject(err)
        }
      })
    })
  }
}
